﻿using MainService;
using MainService.ZNQD;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace StartMain
{
    public partial class ProjectFrm : Form
    {
        public ProjectFrm()
        {
            InitializeComponent();
        }

        private void ProjectFrm_Load(object sender, EventArgs e)
        {
            LoadFrm loader = new LoadFrm();
            SendOrPostCallback spc = new SendOrPostCallback(LoadSolutionList);
            loader.RegisterMesthod(spc, loader);
            loader.ShowDialog();
        }

        private void LoadSolutionList(object obj) 
        {
            LoadFrm loader = obj as LoadFrm;
            try
            {
                loader.SetValueMess(10, "正在获取解决方案列表");
                Solutions solutions = ZNQDServer.GetSolutionList();
                DataGridView dgv = new DataGridView();
                TabPage page = MainTabControl.SelectedTab;
                page.Controls.Add(dgv);
                dgv.Dock = DockStyle.Fill;
                dgv.AutoGenerateColumns = false;
                DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
                column.HeaderText = "解决方案名称";
                column.DataPropertyName = column.Name = "SolutionChinaName";
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                dgv.Columns.Add(column);
                column = new DataGridViewTextBoxColumn();
                column.HeaderText = "版本";
                column.Width = 200;
                column.DataPropertyName = column.Name = "Version";
                dgv.Columns.Add(column);
                column = new DataGridViewTextBoxColumn();
                column.HeaderText = "说明";
                column.DataPropertyName = column.Name = "Description";
                column.Visible = false;
                dgv.Columns.Add(column);
                column = new DataGridViewTextBoxColumn();
                column.HeaderText = "ID";
                column.DataPropertyName = column.Name = "SolutionID";
                column.Visible = false;
                dgv.Columns.Add(column);
                dgv.DataSource = solutions.list;
                dgv.ReadOnly = true;
                dgv.CellDoubleClick += dgv_CellDoubleClick;
                dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgv.MultiSelect = false;
            }
            catch (Exception Err)
            {
                MessageBox.Show(Err.Message);
                MyServer.Log(Err.ToString());
            }
            finally
            {
                loader.Close();
            }
        }

        void dgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dgv = sender as DataGridView;
            DataGridViewRow  row=dgv.SelectedRows[0];
            Solution solution = row.DataBoundItem as Solution;
            AppSystemFrm asf = new AppSystemFrm(solution);
            if (asf.ShowDialog() == DialogResult.OK) 
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void BtnCreate_Click(object sender, EventArgs e)
        {

        }
    }
}
